﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entities;
using Utility;
using BUS;

public partial class A_LetsDeal_thanhtoan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data();
        }
    }

    private void load_data()
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        Tbl_DonHang_Entities hd = Tbl_DonHang_BUS.GetByID(id);
        lit_mahoadon.Text = hd.MaDH;
        lit_khachhang.Text = hd.NguoiDat.ToString();
        lit_ngaydat.Text = hd.NgayDat.ToString();
        Tbl_KhachHang_Entities kh = Tbl_KhachHang_BUS.GetByID(hd.NguoiDat);
        lit_khachhang.Text = kh.HoTen;
        lit_httt.Text = Tbl_HTTT_BUS.GetByID(hd.HTTT).TenHTTT;
        lit_dcnh.Text = hd.DiaChiNhanVoucher;
        litthongbao.Text = "Khách hàng vui lòng điền chính xác các thông tin phía dưới !";
        if (hd.Nguoidungthanhtoan == true)
        {
            chb_dathanhtoan.Checked = true;
        }
        Criteria c = new Criteria();
        int total = 0;
        c.add(Condition.AND, Tbl_CTDH_Columns.DonHang, Condition.EQUAL, id);
        Tbl_CTDH_Collection cthdCollection =
            Tbl_CTDH_BUS.Get(c.Criter, 0, 0,
            Tbl_CTDH_Columns.STT.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = cthdCollection;
        Repeater1.DataBind();

    }
    public float tongtrigia = 0;
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                   e.Item.ItemType == ListItemType.Item)
        {
            Tbl_CTDH_Entities cthd = (Tbl_CTDH_Entities)e.Item.DataItem;

            Tbl_SanPham_Entities sp = Tbl_SanPham_BUS.GetByID(cthd.SanPham);
            Literal lit_tensp = (Literal)e.Item.FindControl("lit_tensp");
            lit_tensp.Text = sp.TenSP;

            Literal lit_soluong = (Literal)e.Item.FindControl("lit_soluong");
            lit_soluong.Text = cthd.SoLuong.ToString();

            Literal lit_dongia = (Literal)e.Item.FindControl("lit_dongia");
            lit_dongia.Text = String.Format("{0:0,0 đ}", sp.GiaBan);

            Literal lit_thanhtien = (Literal)e.Item.FindControl("lit_thanhtien");
            lit_thanhtien.Text = String.Format("{0:0,0 đ}", cthd.SoLuong * sp.GiaBan);
            tongtrigia = tongtrigia + (cthd.SoLuong * sp.GiaBan);


        }
        lit_trigia.Text = String.Format("{0:0,0 }", tongtrigia / 21000);
    }

    protected void bt_timkiem_Click(object sender, EventArgs e)
    {
        if (txt_timkiem.Text != "")
        {
            Response.Redirect("~/A_LetsDeal/Default.aspx?tensanpham=" + txt_timkiem.Text);
        }
    }
    protected void btthanhtoan_Click(object sender, EventArgs e)
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        Tbl_DonHang_Entities hd = Tbl_DonHang_BUS.GetByID(id);
        PayPalGateway pp = new PayPalGateway();
        //PayPalReturn rv = pp.Pay(hd.STT.ToString(), lit_trigia.Text, txtten.Text, txtho.Text, txtdiachi.Text, "HCM", "CA", "US", "United States", "12345", "Visa", txtsothe.Text, txtcvv.Text, txtthang.Text, txtnam.Text);
        //if (rv.IsSucess)
        PayPalReturn rv = pp.Pay(hd.STT.ToString(), lit_trigia.Text.ToString(), txtten.Text, txtho.Text, txtdiachi.Text, "HCM", "TP", "CA", "Việt Nam", "95131", "Visa", txtsothe.Text, txtcvv.Text, txtthang.Text, txtnam.Text);
        if (rv.IsSucess)
        {
            litthongbao.Text = "Thanh toán thành công ! Mã giao dịch của bạn là: " + rv.TransactionID;
            Tbl_DonHang_Entities donhang =Tbl_DonHang_BUS.GetByID(id);
            Tbl_DonHang_BUS.Update_NGUOIDUNGTHANHTOAN(donhang.MaDH);
        }
        else
        {
            litthongbao.Text = "Lỗi xử lý: " + rv.ErrorMessage;
        }
    }
}
